/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2023-04-07 10:19:24
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
using System.Collections.Generic;
using CoreCms.Net.Services;
using AutoMapper.Configuration.Annotations;
using CoreCms.Net.Model.ViewModels.ordermeeting;
using CoreCms.Net.Auth.HttpContextUser;
using Newtonsoft.Json;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 
    ///</summary>
 
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderMeetingGoodsController : ControllerBase
    {
        private readonly IHttpContextUser _user;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly ICoreShopOrderMeetingGoodsServices _CoreShopOrderMeetingGoodsServices;
        private readonly ICoreCmsGoodsServices _coreCmsGoodsServices;
        private readonly ICoreShopOrderMeetingUserServices _coreShopOrderMeetingUserServices;

        private ICoreCmsGoodsCategoryServices _goodsCategoryServices;

        /// <summary>
        /// 构造函数
        ///</summary>
        public OrderMeetingGoodsController(IHttpContextUser user
                 , ICoreCmsGoodsCategoryServices goodsCategoryServices
            ,    IWebHostEnvironment webHostEnvironment
            ,  ICoreCmsGoodsServices coreCmsGoodsServices
            , ICoreShopOrderMeetingGoodsServices CoreShopOrderMeetingGoodsServices
                , ICoreShopOrderMeetingUserServices coreShopOrderMeetingUserServices
            )
        {
            _user=user;
            _goodsCategoryServices = goodsCategoryServices;
            _webHostEnvironment = webHostEnvironment;
            _coreCmsGoodsServices = coreCmsGoodsServices;
            _CoreShopOrderMeetingGoodsServices = CoreShopOrderMeetingGoodsServices;
            _coreShopOrderMeetingUserServices = coreShopOrderMeetingUserServices;
        }

        #region 获取列表============================================================
        // POST: Api/CoreShopOrderMeetingGoods/GetPageList
         /// <summary>
        /// 获取列表，通过分类和订货会id
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取列表")]
        [Authorize]
        public async Task<WebApiCallBack> GetPageList([FromQuery]int meetingid, [FromBody] FMPageByWhereOrder entity)
        {
            var jm = new WebApiCallBack();

            var where = PredicateBuilder.True<CoreShopOrderMeetingGoods>();

            where = where.And(p => p.meetingid == meetingid);
            var className = string.Empty;
            if (!string.IsNullOrWhiteSpace(entity.where))
            {
                var obj = JsonConvert.DeserializeAnonymousType(entity.where, new
                {
                    priceFrom = "",
                    priceTo = "",
                    catId = "",
                    brandId = "",
                    labelId = "",
                    searchName = "",
                });


                if (!string.IsNullOrWhiteSpace(obj.catId))
                {
                    var catId = obj.catId.ObjectToInt(0);
                    if (catId > 0)
                    {
                        var category = await _goodsCategoryServices.QueryByIdAsync(catId);
                        if (category != null)
                        {
                            className = category.name;
                        }

                        var childs = await _goodsCategoryServices.QueryListByClauseAsync(p => p.parentId == catId);
                        if (childs.Any())
                        {
                            var ids = childs.Select(p => p.id).ToList();
                            where = where.And(p => ids.Contains(p.goods.goodsCategoryId) || p.goods.goodsCategoryId == catId);
                        }
                        else
                        {
                            where = where.And(p => p.goods.goodsCategoryId == catId);
                        }
                    }
                }


            }

            


         

            //获取数据
            var list = await _CoreShopOrderMeetingGoodsServices.QueryPageAsync(where,m=>m.id, OrderByType.Asc, entity.page, entity.limit, false);
            //List<CoreCmsGoods> goodslist = new List<CoreCmsGoods>();
            //foreach (var item in list) {
            //    goodslist.Add(item.goods);
            //}
            //返回数据
            jm.data = list;
            jm.code = 0;
      
            jm.msg = "数据调用成功!";
            return jm;
        }
        #endregion

     
 
        #region 预览数据============================================================
        // POST: Api/CoreShopOrderMeetingGoods/GetDetails/10
        /// <summary>
        /// 预览数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("预览数据")]
        [Authorize]
        public async Task<WebApiCallBack> GetDetails([FromBody]FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _CoreShopOrderMeetingGoodsServices.QueryByIdAsync(entity.id, false);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            jm.code = 0;
            jm.data = model;

            return jm;
        }
        #endregion

      

    }
}
